package com.yilin.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.platform.base_core.application.BaseEntity;
import com.yilin.platform.base_core.application.FlowField;
import com.yilin.platform.base_core.commons.utils.FieldUtil;
import com.yilin.platform.base_core.commons.utils.JsonUtil;
import com.yilin.platform.base_core.tools.pab.base.XmlField;
import com.yilin.platform.entity.capital.TradeFlow;
import com.yilin.platform.entity.cmbc.CmbcTradeRecord;
import com.yilin.platform.mapper.capital.TradeFlowMapper;
import com.yilin.platform.service.ITradeFlowService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

@Service
@Transactional
public class TradeFlowService extends ServiceImpl<TradeFlowMapper, TradeFlow> implements ITradeFlowService {

    @Resource
    TradeFlowMapper tradeFlowMapper;

    /**
     * 根据本地流水号和类型查询流水
     */
    @Override
    public TradeFlow getTradeFlowByTradeNoType(String tradeNo, TradeFlow.PaymentType paymentType) {
        QueryWrapper<TradeFlow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("trade_no", tradeNo);
        queryWrapper.eq("payment_type", paymentType.index);
        return tradeFlowMapper.selectOne(queryWrapper);
    }

    /**
     * 生成对账流水
     */
    @Override
    public <T extends BaseEntity> TradeFlow[] generateTradeFlows(String tradeName, TradeFlow.PayType PayType, T tradeRecord) {
        TradeFlow[] tradeFlows = new TradeFlow[2];
        TradeFlow pubTradeFlow = new TradeFlow();
        pubTradeFlow.putFieldByRecord(tradeRecord, "tradeNo");
        pubTradeFlow.setTransName(tradeName);//接口名称+【接口编码】
        pubTradeFlow.putFieldByRecord(tradeRecord, "bankFlowNo");
        pubTradeFlow.putFieldByRecord(tradeRecord, "money");
        pubTradeFlow.putFieldByRecord(tradeRecord, "tradesDate");
        pubTradeFlow.setPayType(PayType);
        //检查本次付款流水存储情况
        TradeFlow localFromFlow = getTradeFlowByTradeNoType(pubTradeFlow.getTradeNo(), TradeFlow.PaymentType.expenditure);
        if (localFromFlow != null) tradeFlows[0] = localFromFlow;
        if (tradeFlows[0] == null) { //构建付款方流水, 平台付款的拥有者就是平台
            tradeFlows[0] = FieldUtil.copy(pubTradeFlow,TradeFlow.class);
            tradeFlows[0].setUpper(TradeFlow.Upper.yes);//构建时默认上送
            tradeFlows[0].setPaymentType(TradeFlow.PaymentType.expenditure);
            tradeFlows[0].putFieldByRecord(tradeRecord, "holderOrganizeId"); //拥有者
            tradeFlows[0].putFieldByRecord(tradeRecord, "holderName"); //拥有者
            tradeFlows[0].putFieldByRecord(tradeRecord, "holderBankAccNo"); //拥有者
            tradeFlows[0].putFieldByRecord(tradeRecord, "targetOrganizeId"); //对方
            tradeFlows[0].putFieldByRecord(tradeRecord, "targetName"); //对方
            tradeFlows[0].putFieldByRecord(tradeRecord, "targetBankAccNo") ;//对方
        }
        //检查本次收款流水存储情况
        TradeFlow localToFlow = getTradeFlowByTradeNoType(pubTradeFlow.getTradeNo(), TradeFlow.PaymentType.income);
        if (localToFlow != null) tradeFlows[1] = localToFlow;
        if (tradeFlows[1] == null) { //构建收款方流水
            tradeFlows[1] = FieldUtil.copy(pubTradeFlow,TradeFlow.class);
            tradeFlows[1].setUpper(TradeFlow.Upper.no);//构建时默认上送
            tradeFlows[1].setPaymentType(TradeFlow.PaymentType.income);
            tradeFlows[1].putFieldByRecord(tradeRecord, "holderOrganizeId","targetOrganizeId");
            tradeFlows[1].putFieldByRecord(tradeRecord, "holderName","targetName");
            tradeFlows[1].putFieldByRecord(tradeRecord, "holderBankAccNo","targetBankAccNo");
            tradeFlows[1].putFieldByRecord(tradeRecord, "targetOrganizeId","holderOrganizeId");
            tradeFlows[1].putFieldByRecord(tradeRecord, "targetName","holderName");
            tradeFlows[1].putFieldByRecord(tradeRecord, "targetBankAccNo","holderBankAccNo");
        }
        return tradeFlows;
    }



}
